Vast amounts of data are transmitted and received every day in a variety of contexts, from smartphones to personal computers to backend data servers. Two major concerns with data are the size of data transmitted and privacy of the data. Data transmitted from one endpoint to another may travel through a variety of intermediate networking hardware, such as routers, switches, servers, Internet service providers (ISPs), and so forth. Each intermediate entity is affected by the size of the data, which consumes resources like memory, CPU, network bandwidth, and cache space, and has the opportunity to snoop on or intercept data.
Data compression and encryption are independent technologies designed to alleviate these problems by reducing data size and ensuring data privacy. As soon as encoding and storing digital information became possible, the need for protecting that information from prying eyes also became useful. In addition, since bandwidth and storage are scarce resources for both analog and digital communications, the need for compressing information to use less space also became a needed commodity. The goal of data compression is to represent information in a more compact form that takes less space to store and transmit. A sender compresses data by noticing redundancies in the data, finding patterns in the data, and applying a variety of other well-known techniques. A receiver uses shared knowledge about the compression method used to decompress the data back into its original, pre-transmission form. Compression may occur at the sender, at intermediate entities between the sender and receiver (e.g., TCP header compression), or anywhere else.
On the other hand, the role of encryption is to protect the confidentiality of the message in case it is intercepted during storage or transit. When discussing compression and encryption algorithms, two algorithms are involved: one that encodes the original message into a different representation, and another one that decodes it to reconstruct the original message, often by reversing the process performed by the encoder. Encryption may use a variety of types of ciphers, keys, and other techniques to perform the function of securing data so that only the sender and receiver know the true content. Compression aims to reduce the size of the message so that less data can be stored or transmitted. Encryption attempts to protect the actual information contained in the message. Therefore, encryption typically does not increase or decrease the size of the message, except for a few padding bytes in the last block.
Since compression and encryption serve different goals, both evolved as separate branches within computing and engineering. Therefore, if the application requires both compression and encryption, they are usually performed in separate, sequential stages. Since the encryption stage has the effect of randomizing the original message, compression is performed first, followed by encryption. Otherwise, the message would compress poorly. Since compression and encryption serve different needs, it is rare and difficult to implement both within the same functional unit. They are typically implemented in sequential stages, requiring at least two passes: one during compression, the other one during encryption. On the decoder end, the compressed/encrypted data is decrypted first followed by decompression, in that strict order.